//index.js
//获取应用实例
const app = getApp();
const root = app.globalData.ROOT_URL;
const utils = require('../../utils/util.js');
Page({
  data: {
     listdata:[],
     page:1
  },
  //事件处理函数
  onLoad: function () {
  },
  onShow: function(){
      if(app.loginState()==true){
          this.updateData(1, false)
      }
  },
  updateData:function(page,state){
      wx.showLoading({
          title: '',
      })
      utils.ajax(root + 'nowdemosts', {
          header: {
              Accept: 'application/vnd.dct.v1+json'
          },
          data: {
              uid: wx.getStorageSync('user_info').uid,
              page: page
          }
      }, (res) => {
          wx.hideLoading()
          if (res.state == '1') {
              if (res.data.length<1){
                //   wx.showToast({
                //       title:'暂时没有更多数据，请稍后重试~',
                //       icon:'none'
                //   })
                  wx.stopPullDownRefresh()
              }else{
                  if (state == true) {
                      this.setData({
                          listdata: this.data.listdata.concat(res.data)
                      })
                  } else {
                      this.setData({
                          listdata: res.data
                      })
                      wx.stopPullDownRefresh()
                  }
              }
          }else{
              wx.showToast({
                  title: '获取数据失败，网络错误',
                  icon:'none'
              })
          }
      },err=>{
          wx.hideLoading()
          wx.showToast({
              title: '获取数据失败，网络错误',
              icon: 'none'
          })
      })
  },
  //登录状态
  loginState: function(){
      let user_Info = wx.getStorageSync('user_info');
      if (user_Info.uid == "" || user_Info.uid == undefined || user_Info.uid == null) {
        wx.redirectTo({
              url: '/pages/views/login/login',
          })
          return false;
      }
      let nowtime = new Date().getTime(), oldtime = Number(user_Info.timeout);
      if (nowtime - oldtime >= 604800000) {
          wx.removeStorageSync('user_info');
          wx.redirectTo({
            url: '/pages/views/login/login',
          })
          return false;
      }
      return true;
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.setData({
          page:1
      })
      this.updateData(1,false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      let p = ++this.data.page;
      this.setData({
          page:p
      })
      this.updateData(this.data.page,true)
  },
  // 点击跳转往期路演
  taphash:function () {
    wx.navigateTo({
      url: '../pastshow/pastshow',
    })
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
})
